package com.qianfeng.shoon.pet.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.qianfeng.shoon.pet.BaseApp;
import com.qianfeng.shoon.pet.R;
import com.qianfeng.shoon.pet.bean.MudidiEntity;
import com.qianfeng.shoon.pet.utils.ImageLoaderUtils;
import com.qianfeng.shoon.pet.weight.RoundCornerImageView;

import java.util.List;

/**
 * Created by admin on 2015/11/11.
 */
public class MudidiAdapter extends  AppBaseAdapter<MudidiEntity.ElementsEntity> {
    private ViewHolder holder;
    private DisplayImageOptions options= ImageLoaderUtils.getDefaultDisplayImageOptions();
    public MudidiAdapter(List<MudidiEntity.ElementsEntity> list, Context context) {
        super(list, context);
    }



    @Override
    public ViewHolder getViewHolder(final int position, View convertView, ViewGroup parent) {
         holder=ViewHolder.get(context,convertView,parent, R.layout.mudidi_list_item);
        TextView tvText= (TextView) holder.findViewById(R.id.tv_text);
        tvText.setText(list.get(position).getTitle());
        RoundCornerImageView roundCornerImageView= (RoundCornerImageView) holder.findViewById(R.id.iv);
        RoundCornerImageView roundCornerImageViewright= (RoundCornerImageView) holder.findViewById(R.id.iv_righttop);
        RoundCornerImageView roundCornerImageViewleftBottom= (RoundCornerImageView) holder.findViewById(R.id.iv_leftbottom);
        RoundCornerImageView roundCornerImageViewrightBottom= (RoundCornerImageView) holder.findViewById(R.id.iv_rightbottom);




         ImageView ivBack= (ImageView) holder.findViewById(R.id.iv_back);

        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
        int size = list.get(position).getData().size();
       if(list.get(position).getMore()==true)
       {
           ivBack.setVisibility(View.VISIBLE);
       }
        else {
           ivBack.setVisibility(View.GONE);
       }
        if(size>3) {

            roundCornerImageViewleftBottom.setVisibility(View.VISIBLE);
            roundCornerImageViewrightBottom.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(list.get(position).getData().get(0).getCover_s(), roundCornerImageView, options);
            ImageLoader.getInstance().displayImage(list.get(position).getData().get(1).getCover_s(), roundCornerImageViewright, options);
            ImageLoader.getInstance().displayImage(list.get(position).getData().get(2).getCover_s(), roundCornerImageViewleftBottom, options);
            ImageLoader.getInstance().displayImage(list.get(position).getData().get(3).getCover_s(), roundCornerImageViewrightBottom, options);


            roundCornerImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.call(list.get(position).getData().get(0).getId(), list.get(position).getData().get(0).getType());
                }
            });
            roundCornerImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.call(list.get(position).getData().get(1).getId(), list.get(position).getData().get(1).getType());
                }
            });
            roundCornerImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.call(list.get(position).getData().get(2).getId(), list.get(position).getData().get(2).getType());
                }
            });
            roundCornerImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                       callBack.call(list.get(position).getData().get(3).getId(),list.get(position).getData().get(3).getType());
                }
            });




        }
        else
        {

            ImageLoader.getInstance().displayImage(list.get(position).getData().get(0).getCover_s(), roundCornerImageView, options);
            ImageLoader.getInstance().displayImage(list.get(position).getData().get(1).getCover_s(), roundCornerImageViewright, options);



            roundCornerImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.call(list.get(position).getData().get(0).getId(), list.get(position).getData().get(0).getType());
                }
            });
            roundCornerImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.call(list.get(position).getData().get(1).getId(), list.get(position).getData().get(1).getType());
                }
            });
            roundCornerImageViewleftBottom.setVisibility(View.GONE);
            roundCornerImageViewrightBottom.setVisibility(View.GONE);
        }


        return holder;
    }



    private CallBack callBack;
    public  interface CallBack
    {
        public  void call(String id,int type);
    }


    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
